package com.example.spring.controller;

import com.example.spring.pojo.Library;
import com.example.spring.service.LibraryService;
import com.example.spring.util.RedisUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class LibraryController {


    //    @Api：修饰整个类，描述Controller的作用
    //    @ApiOperation：描述一个类的一个方法，或者说一个接口
    //    @ApiParam：单个参数描述
    //    @ApiModel：用对象来接收参数
    //    @ApiProperty：用对象接收参数时，描述对象的一个字段
    //    @ApiResponse：HTTP响应其中1个描述
    //    @ApiResponses：HTTP响应整体描述
    //    @ApiIgnore：使用该注解忽略这个API
    //    @ApiError ：发生错误返回的信息
    //    @ApiParamImplicitL：一个请求参数
    //    @ApiParamsImplicit 多个请求参数

    @Autowired
    private LibraryService libraryService;
    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation(value = "获取图书列表", notes = "获取图书列表")
    @RequestMapping(value = "/get_books", method = RequestMethod.GET)
    public List<Library> getAllLibrary() {
        List<Library> libraryList = libraryService.selectLibrary();
        redisUtil.lSet("library:libraryList", libraryList);
        return libraryList;
    }

    @ApiOperation(value = "获取图书", notes = "获取图书")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "图书id", required = true, dataType = "int", paramType = "path")
    })
    @RequestMapping(value = "/get_book/{id}", method = RequestMethod.GET)
    public Library getLibrary(@PathVariable("id") Integer id) {
        Library library = libraryService.getLibrary(id);
        redisUtil.lSet("library:library", library);
        return library;
    }

    @ApiOperation(value = "创建图书", notes = "创建图书")
    @ApiImplicitParam(name = "library", value = "图书详细实体", required = true, dataType = "library")
    @RequestMapping(value = "/add_book", method = RequestMethod.POST)
    public Integer addLibrary(@RequestBody Library library) {
        return libraryService.addLibrary(library);
    }

    @ApiOperation(value = "更新图书信息", notes = "根据url的id来指定更新图书信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "图书id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "position", value = "职位", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/update_book", method = RequestMethod.GET)
    public Integer updateLibrary(String position, Integer id) {
        return libraryService.updateLibrary(position, id);
    }

}
